<?php
namespace Common\Controller;
use Think\Controller;
/**
 * cms项目公共Action
 */
class AdminbaseController extends Controller 
{
    protected $inMenuids;
    protected $menuObj;
    protected $userid;
    protected $ischeckAccess;
    protected $status;
    public function _initialize() 
    {
        $this->menuObj=M('Menu');
        $userObj= D("Site/User");
        $roleObj= D("User_role");
        $this->status = array('gt',0);
        if(isset($_SESSION['ADMIN_ID'])){
            if ($_SESSION['ADMIN_ID']==="administrator") {
                $this->ischeckAccess=false;
            }else{
                $this->ischeckAccess=true;
                $this->userid=$_SESSION['ADMIN_ID'];
                $user=$userObj->relation('role')->find($this->userid);
                $this->inMenuids=$user['role']['menuids'];
                $this->checkAccess($this->inMenuids);
            }
        }else{
            //$this->error("您还没有登录！",U("admin/public/login"));
            if(IS_AJAX){
                $this->error("您还没有登录！",U("admin/index/login"));
            }else{
                header("Location:".U("admin/index/login"));
                exit();
            }
        }
    }

    //获取表单令牌
    protected function getToken() {
        $tokenName = C('TOKEN_NAME');
        // 标识当前页面唯一性
        $tokenKey = md5($_SERVER['REQUEST_URI']);
        $tokenAray = session($tokenName);
        //获取令牌
        $tokenValue = $tokenAray[$tokenKey];
        return $tokenKey . '_' . $tokenValue;
    }

     /**
     * 获取顶部菜单导航
     * @param type $app
     * @param type $model
     * @param type $action
     */
    public  function getParentMenu($type=1) 
    {
        $condition['parentid']=0;
        $condition['status']=1;
        if($this->ischeckAccess) $condition['id']=array("in",$this->inMenuids);
        $parentMenuObj = $this->menuObj->cache(true, 60)->where($condition)->order(array("listorder"=>"asc","id"=>"asc"))->getField("id,action,controller,model,name,icon");
        return $parentMenuObj;
    }
     /**
     * 获取子菜单导航
     * @param type $app
     * @param type $model
     * @param type $action
     */
    public  function getChildrenMenu($model,$type=1) 
    {
        $childrenMenuObj=array();
        $parentId = $this->menuObj->where(array("model"=>$model,"parentid"=>0,"status"=> 1,))->getField("id");
        $condition['parentid']=$parentId;
        $condition['status']=1;
        if($this->ischeckAccess) $condition['id']=array("in",$this->inMenuids);
        $firstMenuObjs = $this->menuObj->where($condition)->order(array("listorder"=>"asc","id"=>"asc"))->select();
        if (!empty($firstMenuObjs)) {
            $childrenMenuObj['firstMenu']=$firstMenuObjs;
            foreach ($firstMenuObjs as $key => $firstMenuObj) {
                $parentid=$firstMenuObj['id'];
                $seccondMenuObjs = $this->menuObj->where(array("parentid"=>$parentid,"status"=> 1,"type"=>$type))->order(array("listorder"=>"asc","id"=>"asc"))->select();
                if (!empty($seccondMenuObjs)) {
                    $childrenMenuObj['firstMenu'][$key]['seccondMenu'] = $seccondMenuObjs;
                }
            }
        }
        return $childrenMenuObj;
    }
    public function checkAccess($userid)
    {
        $condition['model']=$Think.MODULE_NAME;
        $condition['controller']=$Think.CONTROLLER_NAME;
        $condition['status']=1;
        $condition['id']=array("in",$this->inMenuids);
        $menu = $this->menuObj->where($condition)->select();
        if (empty($menu)) {
            $this->error("对不起，你无权限操作！",U("admin/index/index"));
        }
    }
    public function _listorder($model,$ids,$listorders)
    {
        if (!empty($ids)) 
        {
           foreach ($ids as $key => $id) 
           {
                $data['listorder']=$listorders[$key];
                $model->where("id=%d",$id)->save($data);
           }
           $arr = array("status"=>'1',"message"=>'排序更新成功！');
        }
        else
        {
           $arr = array("status"=>'0',"message"=>'获取数据错误！');
        }
        $json_str = json_encode($arr);
        echo $json_str;
    }

    public function _delete($model,$id)
    {
        if($id!=null&&$id>0){
            $result = $model->delete($id);
            if ($result) {
                 $arr = array("status"=>'1',"message"=>'删除数据成功！');
            } else {
                 $arr = array("status"=>'0',"message"=>'删除数据失败！');
            }
        }else{
             $arr = array("status"=>'0',"message"=>'获取数据错误！');
        }
        $json_str = json_encode($arr);
        echo $json_str;
    }

    /* 批量删除 ids*/
    public function _checkDeletes($model,$ids){
        if($ids!=null&&$ids.length>0){
            $result = $model->delete($ids);
            if ($result) {
                 $arr = array("status"=>'1',"message"=>'批量删除数据成功！');
            } else {
                 $arr = array("status"=>'0',"message"=>'批量删除数据失败！');
            }
        }else{
             $arr = array("status"=>'0',"message"=>'获取数据错误！');
        }
        $json_str = json_encode($arr);
        echo $json_str;
    }
    /* 修改当前状态status*/
    public function _changeStatus($model,$id,$status){
        if($id!=null&&$id>0){
             $data=array();
             $data['status'] = $status;
             $result = $model->where("id=%d",$id)->save($data);
             if ($result) {
                 $arr = array("status"=>'1',"message"=>'修改状态成功！');
             } else {
                 $arr = array("status"=>'0',"message"=>'修改状态失败！');
             }
         }else{
            $arr = array("status"=>'0',"message"=>'获取数据错误！');
         }
        
         $json_str = json_encode($arr);
         echo $json_str;
    }
    /*批量 修改为回收站*/
    public function _checkChanges($model,$status,$ids){

      if($ids!=null&&$ids!=""){
           $map['id']  = array('in',$ids);
           $data['status'] = $status;
           $result=$model->where($map)->save($data);
           if ($result) {
                 $arr = array("status"=>'1',"message"=>'批量操作成功！');
            } else {
                 $arr = array("status"=>'0',"message"=>'批量操作失败');
            }
        }else{
             $arr = array("status"=>'0',"message"=>'获取数据错误！');
        }
        $json_str = json_encode($arr);
        echo $json_str;
    }
}

?>
